package com.budwk.app.sqfwV2.generator.wasteCategory.excel;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.SignImageComponent;
import com.budwk.app.sqfwV2.component.WasteCategoryComponent;
import com.budwk.app.sqfwV2.dto.dto.WasteCategoryNewPdfDTO;
import com.budwk.app.sqfwV2.dto.image.ExcelProblemImageConfigBO;
import com.budwk.app.sqfwV2.enums.WasteCategoryFileServiceEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 垃圾分类
 */

@Slf4j
public class RefuseClassifyRecGenerator extends BaseTemplateExcelGenerator {

    @Override
    protected String getTemplateName() {
        return WasteCategoryFileServiceEnum.ONE_EXCEL.getFilePath();
    }

    @Override
    public String getDownloadFileName() {
        return WasteCategoryFileServiceEnum.ONE_EXCEL.getFileName();
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     * @param excelParam 已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        log.info("RefuseClassifyRecGenerator getBaseExcelParamDTO: {}", JSONObject.toJSONString(excelCreateParam.getBaseExcelParamDTO()));
        BaseExcelParamDTO baseParam = excelCreateParam.getBaseExcelParamDTO();
        excelParam.put("unitName", baseParam.getUnitName());
        excelParam.put("downloadDate", DateUtil.format(new Date(), "yyyy 年 MM 月 dd 日"));
    }

    /**
     * 构造内容体
     * @param listMap
     * @param excelCreateParam
     */
    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("RefuseClassifyRecGenerator customerDataParam summaryList:  {}", JSONObject.toJSONString(summaryList));
        // 取出不是云岩街道的所有数据
        List<Sqfw_formSummary_new> datas = summaryList.stream().filter(e -> !e.getCommunity().equals(CommonConstants.YUNTAN_STREET)).collect(Collectors.toList());
        int num = 1;
        for (Sqfw_formSummary_new summary : datas) {
            WasteCategoryNewPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), WasteCategoryNewPdfDTO.class);
            Map<String, Object> map = buildExcelMap(roadPdfDTO);
            map.put("num", num ++);
            listMap.add(map);
        }
        log.info("RefuseClassifyRecGenerator customerDataParam end: {}", JSONObject.toJSONString(listMap));
    }

    public boolean isGenerate() {
        return !WasteCategoryComponent.isYunTanUser();
    }

    private Map<String, Object> buildExcelMap(WasteCategoryNewPdfDTO roadPdfDTO) {
        Map<String, Object> map = new HashMap<>();
        map.put("fillDate", LocalDateTimeUtil.format(roadPdfDTO.getFillDate(), DatePattern.CHINESE_DATE_PATTERN));
        map.put("address", StrUtil.replaceNullStr(roadPdfDTO.getAddress()));
        map.put("problems", StrUtil.replaceNullStr(roadPdfDTO.getProblems()));
        map.put("rectificationType", StrUtil.replaceNullStr(roadPdfDTO.getRectificationType()));
        SignImageComponent.signExcel(map, null, roadPdfDTO.getSitePhotos(), "无", "riskImages", "riskImagesStr");
        return map;
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return ExcelProblemImageConfigBO.class;
    }
}
